package org.millioncall.yueyoga.admin.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nl.captcha.Captcha;
import nl.captcha.backgrounds.GradiatedBackgroundProducer;
import nl.captcha.text.producer.DefaultTextProducer;

import org.millioncall.openplatform.util.CaptchaUtils;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.Hall;
import org.millioncall.yueyoga.admin.service.HallService;
import org.millioncall.yueyoga.common.GlobalConstants;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.WebAttributes;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

//created by zangjy on 2012-3-23
@Controller
public class LoginAction extends BaseAction {
	private HallService hallService;

	public HallService getHallService() {
		return hallService;
	}

	public void setHallService(HallService hallService) {
		this.hallService = hallService;
	}

	// 转向登录页
	@RequestMapping(value = "/toLogin.jhtml")
	public String toLogin(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {

		super.initFrontHeader(request, model);

		List<Hall> halls = hallService.findHalls();
		model.put("halls", halls);
		return "login";
	}

	// 注销用户，转向登录页
	@RequestMapping(value = "/logout.jhtml")
	public String logout(HttpServletRequest request) {
		request.getSession().removeAttribute(GlobalConstants.LOGIN_USER_KEY);
		return "redirect:toLogin.jhtml";
	}

	// 登录认证成功
	@RequestMapping(value = "/loginsuccess.jhtml")
	public String loginSuccess(Map<String, Object> map,
			HttpServletResponse response) {
		return "redirect:main.jhtml";
	}

	// 登录认证失败
	@RequestMapping(value = "/loginfailure.jhtml")
	public String loginFailure(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		String msg = "身份认证失败，请稍候重试";
		AuthenticationException exception = (AuthenticationException) request
				.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
		if (exception != null) {
			msg = exception.getMessage();
		}
		model.put("msg", msg);
		return this.toLogin(model, request, response);
	}

	// 取得机构名称
	@RequestMapping(value = "/ajaxgetorganization.do")
	public void getOrganization(HttpServletResponse response) {
		String msg = "[{'orgid':'org001','orgname':'"
				+ GlobalConstants.APP_ORGANIZATION + "'}]";
		WebAppUtils.ajaxResponse(response, msg);
	}

	// 生成验证码
	@RequestMapping(value = "/getcaptcha.jhtml")
	public void getCaptcha(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			HttpSession session = request.getSession();
			// 以下三个参数也可以通过get参数传值
			boolean autoRefresh = true;// 页面刷新时，是否刷新验证码
			int width = 170;// 验证码图片宽度
			int height = 45;// 验证码图片高度

			Captcha captcha = (Captcha) session
					.getAttribute(CaptchaUtils.WEBAPP_CAPTCHA_KEY);
			if (autoRefresh || captcha == null) {
				captcha = new Captcha.Builder(width, height)
						.addBackground(new GradiatedBackgroundProducer())
						.addText(new DefaultTextProducer()).gimp().addNoise()
						.addNoise().build();
				session.setAttribute(CaptchaUtils.WEBAPP_CAPTCHA_KEY, captcha);
			}
			CaptchaUtils.writeImage(response.getOutputStream(),
					captcha.getImage(), CaptchaUtils.IMAGE_TYPE_PNG);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
